﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;

namespace Dynamico.Service
{
    /// <summary>
    /// //TODO REMOVE IT after adding reference to Shop.Utils (Shop.CommonLib poject)
    /// </summary>
    public class Utils
    {

        public static string GetEncodedName(string nameToEncode)
        {
            StringBuilder cleanName = new StringBuilder(String.Empty);

            if (nameToEncode != null)
            {
                ////string cleanName = Regex.Replace(nameToEncode, @"A-z0-9\s-", "-"); // invalid chars
                MatchCollection cleanCollection = Regex.Matches(nameToEncode, @"[A-z0-9\P{IsBasicLatin}]+");

                string match = String.Empty;
                for (var i = 0; i < cleanCollection.Count; i++)
                {
                    match = cleanCollection[i].ToString();

                    if (!String.IsNullOrEmpty(match))
                    {
                        cleanName.Append(match);

                        //if not last, put "-"
                        if (i != cleanCollection.Count - 1) cleanName.Append("-");
                    }
                }
            }
            //To Lower
            string cleannameString = cleanName.ToString().ToLower()
                //ą 	ć 	ę 	ł 	ń 	ó 	ś 	ź 	ż
                .Replace("ą", "a")
                .Replace("ć", "c")
                .Replace("ę", "e")
                .Replace("ł", "l")
                .Replace("ń", "n")
                .Replace("ó", "o")
                .Replace("ź", "z")
                .Replace("ź", "z");

            return RemoveAccents(cleannameString);
        }

        //source: http://stackoverflow.com/questions/810345/seo-title-making-function
        private static string RemoveAccents(string input)
        {
            string normalized = input.Normalize(NormalizationForm.FormKD);

            Encoding removal = Encoding.GetEncoding
                (Encoding.ASCII.CodePage,
                new EncoderReplacementFallback(""),
                new DecoderReplacementFallback(""));
            byte[] bytes = removal.GetBytes(normalized);
            return Encoding.ASCII.GetString(bytes);
        }


        public static string[] GetTopFlourPrice(string price) {

            string[] ret = new string[3];
            ret[0] = "0";
            ret[1] = "00";
            ret[2] = ",00";
            string newPrice = price.Replace("�z�", "").Replace("�", "").Replace(" zł", "").Trim();
            

            if (String.IsNullOrEmpty(newPrice))
            {
                ret[0] = "0";
                ret[1] = "00";
                ret[2] = ",00";
            }
            else
            {
                ret[0] = newPrice;
                var priceArray = newPrice.Split(',');
                //top
                 ret[1]  = priceArray[0];
                //floor
                 ret[2] = ",00";
                if (priceArray.Count() > 1)
                {
                    ret[2] = "," + priceArray[1];
                }
            }
            return ret;
        }
    }
}